package com.msmall.community.baby.preparer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.common.model.Commend;

public class BabyIndexHotPreparer implements ViewPreparer{
	
	private static final Log log = LogFactory.getLog(BabyIndexHotPreparer.class);
	
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			
			List<Commend> commends = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_QUESTION_COMMEND_IMAGES);
			request.setAttribute("babyIndexQuestionImage", commends!=null&&commends.size()>0?commends.get(0):null);
			
			List<Commend> list = new ArrayList<Commend>();
			Map<Long,List<Commend>> commendx = (Map<Long,List<Commend>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_QUESTION_COMMEND_HOT);
			for(List<Commend> commendlist : commendx.values()){
				if(commendlist != null && commendlist.size() > 0){
					list.add(commendlist.get(0));
				}
			}
			request.setAttribute("babyIndexQuestion", list);
			
			List<Commend> babyIndexHotImage = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_INDEX_COMMEND_HOT_IMAGE);
			request.setAttribute("babyIndexHotImage", babyIndexHotImage!=null&&babyIndexHotImage.size()>0?babyIndexHotImage.get(0):null);
		}catch(Exception e){
			log.debug(e);
		}
	}
}
